package com.pureut.storage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pureut.storage.domain.ProductionMaterialReturn;
import com.pureut.storage.domain.ProductionMaterialReturnDetail;
import com.pureut.storage.dto.ProductionMaterialReturnDetailDto;
import com.pureut.storage.mapper.ProductionMaterialReturnDetailMapper;
import com.pureut.storage.mapper.ProductionMaterialReturnMapper;
import com.pureut.storage.service.ProductionMaterialReturnDetailService;
import com.sale.common.core.constant.CacheNames;
import com.sale.common.dict.utils.DictUtils;
import com.sale.common.mybatis.core.page.PageQuery;
import com.sale.common.mybatis.core.page.TableDataInfo;
import com.sale.common.redis.utils.CacheUtils;
import com.sale.system.api.RemoteUnitService;
import com.sale.system.api.domain.SysAuxiliary;
import com.sale.system.api.domain.SysDictData;
import com.sale.system.api.model.MaterialUnitDto;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/***
 * 销售退货实现层
 * Author:C
 * Date:2023/01/29 10:39
 * @DESC
 */
@Service
public class ProductionMaterialReturnDetailServiceImpl extends ServiceImpl<ProductionMaterialReturnDetailMapper, ProductionMaterialReturnDetail> implements ProductionMaterialReturnDetailService {

    @Resource
    ProductionMaterialReturnDetailMapper productionMaterialReturnDetailMapper;

    @Resource
    ProductionMaterialReturnMapper productionMaterialReturnMapper;

    @DubboReference
    RemoteUnitService remoteUnitService;


    /**
     * 生产退料明细列表
     *
     * @param id
     * @return
     */
    @Override
    public TableDataInfo<ProductionMaterialReturnDetailDto> getProductionDetail(long id, PageQuery pageQuery) {
        //明细状态
        List<SysDictData> statusArray = DictUtils.getDictCache("miscellaneous_receipt_detail_status");
        Map<String, String> statusMap = statusArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));
        //单位集合
        Map<String, MaterialUnitDto> unitByCodeToMap = remoteUnitService.getUnitByCodeToMap();

        Page<ProductionMaterialReturnDetailDto> productionDetail = productionMaterialReturnDetailMapper.getProductionDetail(id,pageQuery.build());
        List<ProductionMaterialReturnDetailDto> records = productionDetail.getRecords();
        ProductionMaterialReturn productionMaterialReturn = productionMaterialReturnMapper.selectOne(new QueryWrapper<ProductionMaterialReturn>()
            .lambda().eq(ProductionMaterialReturn::getId, id));

        for (ProductionMaterialReturnDetailDto entity : records) {
            //物料单位
            if (entity.getMaterialUnit() != null) {
                entity.setUnitDict(unitByCodeToMap.get(entity.getMaterialUnit()).getUnitName());
            }
            entity.setStatusDict(statusMap.get(entity.getStatus()));
            //辅助属性
            List<SysAuxiliary> statsArray = CacheUtils.get(CacheNames.SYS_GINSENG + "_" + entity.getTenantId(), String.valueOf(entity.getMaterialId()));
            entity.setAuxiliaryAttribute(statsArray);
        }
        return TableDataInfo.build(productionDetail);
    }
}
